package cn.wolfcode.web.controller;

import cn.wolfcode.domain.ConsumptionItem;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.service.IConsumptionItemService;
import cn.wolfcode.util.RequirePermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/consumptionItem")
public class ConsumptionItemController {
    @Autowired
    private IConsumptionItemService consumptionItemService;


    @RequestMapping("/list")
    @RequirePermission(name = "结算单明细查询",expression = "consumptionItem:list")
    public String list(Model model) {
        model.addAttribute("pageInfo", consumptionItemService.listAll());
        return "consumption/list";
    }

    @RequestMapping("/delete")
    @RequirePermission(name = "结算单明细删除",expression = "consumptionItem:delete")
    public String delete(Long id) {
        if (id != null) {
            consumptionItemService.delete(id);
        }
        return "redirect:/consumptionItem/list";
    }

    /*@RequestMapping("/saveOrUpdate")
    @RequirePermission(name = "结算单明细编辑",expression = "consumptionItem:saveOrUpdate")
    public String saveOrUpdate(ConsumptionItem consumptionItem) {
        if (consumptionItem.getId() != null) {
            consumptionItemService.update(consumptionItem);
        } else {
            consumptionItemService.insert(consumptionItem);
        }
        return "redirect:/consumption/list";
    }*/

    // 异步提交表单方式
    @RequestMapping("/saveOrUpdate")
    @RequirePermission(name = "结算单明细编辑", expression = "consumptionItem:saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(ConsumptionItem consumptionItem) {
        if (consumptionItem.getId() != null) {
            consumptionItemService.update(consumptionItem);
        } else {
            consumptionItemService.insert(consumptionItem);
        }
        return new JsonResult(true,"添加成功");
    }
}
